// TorrentSimilarityMapper.java
package com.example.demo.mapper;

import com.example.demo.model.recommendation.TorrentSimilarity;
import com.example.demo.model.recommendation.UserSimilarity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface TorrentSimilarityMapper {
    @Insert("INSERT INTO torrent_similarity (torrent_id1, torrent_id2, similarity) " +
            "VALUES (#{torrentId1}, #{torrentId2}, #{similarity}) " +
            "ON DUPLICATE KEY UPDATE similarity = VALUES(similarity)")
    void upsertSimilarity(TorrentSimilarity similarity);

    @Insert("INSERT INTO user_similarity (user_id1, user_id2, similarity) " +
            "VALUES (#{userId1}, #{userId2}, #{similarity}) " +
            "ON DUPLICATE KEY UPDATE similarity = VALUES(similarity)")
    void upsertUserSimilarity(UserSimilarity similarity);

    @Select("SELECT torrent_id2 AS similarTorrentId, similarity " +
            "FROM torrent_similarity " +
            "WHERE torrent_id1 = #{torrentId} " +
            "ORDER BY similarity DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> findSimilarTorrents(@Param("torrentId") Long torrentId,
                                                  @Param("limit") int limit);
}